package com.neusoft.weekreport.component.controller;


import com.neusoft.weekreport.component.bizService.ProjectBizService;
import com.neusoft.weekreport.component.req.GetAddProjectInfoReq;
import com.neusoft.weekreport.component.req.GetProductInfoListReq;
import com.neusoft.weekreport.component.req.GetProjectInfoListReq;
import com.neusoft.weekreport.component.resp.GetProjectInfoByIdResp;
import com.neusoft.weekreport.component.resp.GetProjectInfoListResp;
import com.neusoft.weekreport.component.resp.ResponseBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@EnableTransactionManagement
@RequestMapping("/project")
public class ProjectController {

    Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    ProjectBizService projectBizService;

    //查询项目列表
    @RequestMapping(value = "/projectManage",method = RequestMethod.GET)
    public ResponseBean<GetProjectInfoListResp> getProjectInfoList(GetProjectInfoListReq req, HttpServletRequest request){
        ResponseBean<GetProjectInfoListResp> responseBean = new ResponseBean<>();
        try{
            GetProjectInfoListResp resp = projectBizService.getProjectInfoList(req);
            responseBean = new ResponseBean(HttpStatus.OK.value(),"查询项目列表成功",resp);
        }catch (Exception e){
            responseBean.setCode(HttpStatus.INTERNAL_SERVER_ERROR.value());
            responseBean.setMsg(HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase());
        }
        return  responseBean;
    }

    //添加项目
    @RequestMapping(value = "/projectManage",method = RequestMethod.POST)
    public ResponseBean addProjectInfo(@RequestBody GetAddProjectInfoReq req, HttpServletRequest request){
        ResponseBean responseBean = new ResponseBean<>();
        try{
            projectBizService.addProjectInfo(req);
            responseBean = new ResponseBean(HttpStatus.OK.value(),"添加项目成功",responseBean);
        }catch (Exception e){
            responseBean.setCode(HttpStatus.INTERNAL_SERVER_ERROR.value());
            responseBean.setMsg(HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase());
        }
        return  responseBean;
    }

    //修改项目
    @RequestMapping(value = "/projectManage",method = RequestMethod.PUT)
    public ResponseBean modifyProjectInfoById(@RequestBody GetAddProjectInfoReq req, HttpServletRequest request){
        ResponseBean responseBean = new ResponseBean<>();
        try{
            projectBizService.modifyProjectInfoById(req);
            responseBean = new ResponseBean(HttpStatus.OK.value(),"修改项目成功",responseBean);
        }catch (Exception e){
            responseBean.setCode(HttpStatus.INTERNAL_SERVER_ERROR.value());
            responseBean.setMsg(HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase());
        }
        return  responseBean;
    }
    //删除项目
    @RequestMapping(value = "/projectManage",method = RequestMethod.DELETE)
    public ResponseBean deleteProjectById(@RequestBody GetAddProjectInfoReq req, HttpServletRequest request){
        ResponseBean responseBean = new ResponseBean<>();
        try{
            projectBizService.deleteProjectById(req);
            responseBean = new ResponseBean(HttpStatus.OK.value(),"删除项目成功",responseBean);
        }catch (Exception e){
            responseBean.setCode(HttpStatus.INTERNAL_SERVER_ERROR.value());
            responseBean.setMsg(HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase());
        }
        return  responseBean;
    }

    //查看项目
    @RequestMapping(value = "/projectInfoById",method = RequestMethod.GET)
    public ResponseBean<GetProjectInfoByIdResp> getProjectInfoById(GetAddProjectInfoReq req, HttpServletRequest request){
        ResponseBean<GetProjectInfoByIdResp> responseBean = new ResponseBean<>();
        try{
            GetProjectInfoByIdResp resp = projectBizService.getProjectInfoById(req);
            responseBean = new ResponseBean(HttpStatus.OK.value(),"查看项目成功",resp);
        }catch (Exception e){
            responseBean.setCode(HttpStatus.INTERNAL_SERVER_ERROR.value());
            responseBean.setMsg(HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase());
        }
        return  responseBean;
    }

    //改变项目状态
    @RequestMapping(value = "/changeProjectStatus",method = RequestMethod.PUT)
    public ResponseBean changeProjectStatusById(@RequestBody GetAddProjectInfoReq req, HttpServletRequest request){
        ResponseBean responseBean = new ResponseBean<>();
        try{
            projectBizService.changeProjectStatusById(req);
            responseBean = new ResponseBean(HttpStatus.OK.value(),"改变项目状态成功",responseBean);
        }catch (Exception e){
            responseBean.setCode(HttpStatus.INTERNAL_SERVER_ERROR.value());
            responseBean.setMsg(HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase());
        }
        return  responseBean;
    }

    //查询有效项目
    @RequestMapping(value = "/validProject",method = RequestMethod.GET)
    public ResponseBean<GetProjectInfoListResp> getValidProject(GetProjectInfoListReq req,HttpServletRequest request){
        ResponseBean<GetProjectInfoListResp> responseBean = new ResponseBean<>();
        try{
            GetProjectInfoListResp resp = projectBizService.getValidProjectInfoList(req);
            responseBean = new ResponseBean(HttpStatus.OK.value(),"查询有效项目列表成功",resp);
        }catch (Exception e){
            responseBean.setCode(HttpStatus.INTERNAL_SERVER_ERROR.value());
            responseBean.setMsg(HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase());
        }
        return  responseBean;
    }
}
